package cn.edu.gdut.deepcode.service.impl;

import cn.edu.gdut.deepcode.annotation.CanvasIdCheckPoint;
import cn.edu.gdut.deepcode.component.GlobalIdGenerator;
import cn.edu.gdut.deepcode.constant.CacheConstant;
import cn.edu.gdut.deepcode.domain.dto.CanvasOperationDTO;
import cn.edu.gdut.deepcode.domain.po.CanvasOperation;
import cn.edu.gdut.deepcode.domain.po.Result;
import cn.edu.gdut.deepcode.enums.OperationTypeEnum;
import cn.edu.gdut.deepcode.mapper.CanvasOperationMapper;
import cn.edu.gdut.deepcode.service.ICanvasOperationService;
import cn.edu.gdut.deepcode.context.UserContext;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 画布操作表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-04-11
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class CanvasOperationServiceImpl extends ServiceImpl<CanvasOperationMapper, CanvasOperation> implements ICanvasOperationService {

    private final GlobalIdGenerator globalIdGenerator;
    @Override
    @CanvasIdCheckPoint
    public Result<Long> submitElement(CanvasOperationDTO canvasOperationDTO) {
        Long elementId=null;
        Long version=null;
        Long canvasId = canvasOperationDTO.getCanvasId();
        if (canvasOperationDTO.getOperationType()== OperationTypeEnum.INSERT){
            // 如果为新增则生成elementId,version
            canvasOperationDTO.setElementId(elementId=globalIdGenerator
                    .generate(CacheConstant.GLOBAL_ID_CANVAS_ELEMENT_KEY_PREFIX+ canvasId));

//            canvasOperationDTO.setVersion(version=globalIdGenerator
//                    .generate(CacheConstant.CANVAS_ELEMENT_VERSION_KEY_PREFIX +elementId));// 先不用后端来生成版本号
        }
        // dto->po,添加操作者id,存储po对象
        save(BeanUtil
                .copyProperties(canvasOperationDTO, CanvasOperation.class)
                .setOperationUserId(UserContext.getUser())
        );
        // 返回结果
        return Result.success(elementId);
//        return Result.success(new CanvasOperationVO()
//                .setElementId(elementId)
//                .setVersion(version)// 先不用后端来生成版本号
//        );
    }
}
